<template>
  <div class="box5">
    <div class="title">
      <p></p>
      <img src="../../images/dataScreen-title.png" alt="" />
    </div>
    <div style="color: white; font-size: 19px; margin-left: 10px">
      实时购买用户
    </div>
    <div class="charts" ref="line">
      <div class="body" :style="{ height: height }">
        <p>
          <span style="color: yellowgreen">{{ wsData?.name }}</span>
          购买了
          <span style="color: #29fcff">{{ wsData?.movie }}</span>
          共
          <span style="color: #ff4949">{{ wsData?.money }}</span>
          元
        </p>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
import * as process from 'node:process'

let ws = undefined
const wsData = ref()
//获取图形图标的节点
let line = ref()
let height = ref()
onMounted(() => {
  init()
})
const init = () => {
  //判断浏览器是否支持WebSocket
  if (typeof WebSocket === 'undefined') {
    alert('您的浏览器不支持socket')
  }

  //在开发环境中，wsUrl为ws://localhost:7073/ws/3
  //在生产环境中，wsUrl为wss://moive-back.xuxiao888.com/ws/3
  const wsUrl = import.meta.env.VITE_WS_URL
  ws = new WebSocket(wsUrl)

  // 监听是否连接成功
  ws.onopen = () => {
    console.log('ws连接状态成功：' + ws.readyState)
    // 连接成功则发送一个数据
    // this.ws.send('连接成功')
  }

  // 接听服务器发回的信息并处理展示
  ws.onmessage = (data) => {
    console.log('接收到来自服务器的消息：')
    height.value = '0'
    setTimeout(() => {
      height.value = '100%'
      wsData.value = JSON.parse(data.data)
    }, 1010)

    console.log(wsData)
  }
  // 监听连接关闭事件
  ws.onclose = () => {
    // 监听整个过程中websocket的状态
    console.log('ws连接状态关闭：' + ws.readyState)
  }

  // 监听并处理error事件
  ws.onerror = function (error) {
    console.log(error)
  }
}
//销毁ws监听
onBeforeUnmount(() => {
  ws.close()
})
</script>

<style scoped lang="scss">
.box5 {
  width: 100%;
  height: 100%;
  background: url(../../images/dataScreen-main-cb.png) no-repeat;
  background-size: 100% 100%;
  margin: 0px 20px;

  .title {
    margin-left: 10px;

    p {
      color: white;
      font-size: 20px;
    }
  }

  .charts {
    height: calc(100% - 40px);

    .body {
      height: 0;
      width: 100%;
      overflow: hidden;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      font-size: 30px;
      color: white;
      transition: all 1s ease-in-out;
    }
  }
}
</style>
